package com.test.nnooolwdeaer.bean_wooden.config_info_wooden;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.ThreadUtil_wooden;
import com.google.gson.Gson;
import com.google.gson.JsonArray;

import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class AppShowInfo_wooden {

    private static class Inner_wooden {
        private static AppShowInfo_wooden instance_wooden = new AppShowInfo_wooden();
    }

    private AppShowInfo_wooden() {
    }

    public static AppShowInfo_wooden getInstance() {
        return Inner_wooden.instance_wooden;
    }

    private Customize customize;
    private ArrayList<WalletChannel> walletChannels;

    public static void parse_wooden(JSONObject objJson) {
        //if (objJson==null) return;
        Gson gson = new Gson();
        Inner_wooden.instance_wooden = gson.fromJson(objJson.toString(), AppShowInfo_wooden.class);

        if (Inner_wooden.instance_wooden.customize == null)
            Inner_wooden.instance_wooden.customize = gson.fromJson(objJson.optString("customize"), Customize.class);
    }

    /**
     * 配置的AF key
     */
    public String getAFKey_wooden() {
        if (customize == null) return "";
        return customize.getAfKey();
    }

    public String[] getMobile_wooden() {
        if (customize == null) return new String[0];
        return customize.getMobile();
    }

    public boolean showSysReview() {
        if (customize == null) return false;
        return customize.showSysReview();
    }

    public String[] getEmail_wooden() {
        if (customize == null) return new String[0];
        return customize.getEmail();
    }

    public String getTransferDelayImportant_wooden() {
        if (customize == null) return "";
        return StringUtil_wooden.getSafeString_wooden(customize.getTransferDelayImportant());
    }

    public String getRejectPeriod() {
        if (customize == null) return "";
        return customize.getRejectPeriod();
    }

    public String getSignHost() {
        if (customize == null || customize.signMsg == null) return "";
        return customize.signMsg.getHost();
    }

    public String getSignScheme() {
        if (customize == null || customize.signMsg == null) return "";
        return customize.signMsg.getScheme();
    }

    public String getSignPck() {
        if (customize == null || customize.signMsg == null) return "";
        return customize.signMsg.getPck();
    }

    public String getSignAppName() {
        if (customize == null || customize.signMsg == null) return "";
        return customize.signMsg.getAppName();
    }

    public String getSignDevName() {
        if (customize == null || customize.signMsg == null) return "";
        return customize.signMsg.getDevName();
    }

    public String getSignLogo() {
        if (customize == null || customize.signMsg == null) return "";
        return customize.signMsg.getLogo();
    }

    public String getSignUrl() {
        if (customize == null || customize.signMsg == null) return "";
        return customize.signMsg.getUrl();
    }

    public String getSignScore() {
        if (customize == null || customize.signMsg == null) return "";
        return customize.signMsg.getScore();
    }

    public String getSignAge() {
        if (customize == null || customize.signMsg == null) return "";
        return customize.signMsg.getAge();
    }

    public String getSignDownloads() {
        if (customize == null || customize.signMsg == null) return "";
        return customize.signMsg.getDownloads();
    }

    public String getCustomerDesc() {
        if (customize == null) return "";
        return customize.getCustomerDesc();
    }

    public int minVersion() {
        if (customize == null) return 1;
        return customize.minVersion();
    }

    public int lastVersion() {
        if (customize == null) return 1;
        return customize.lastVersion();
    }

    public String lastVersionName() {
        if (customize == null) return "1.0";
        return customize.lastVersionName();
    }

    public String lastVersionDesc() {
        if (customize == null) return "";
        return customize.lastVersionDesc();
    }

    public String repayNotice() {
        if (customize == null) return "";
        return customize.repayNotice();
    }

    public String loaningNotice() {
        if (customize == null) return "";
        return customize.loaningNotice();
    }

    public String reviewNotice() {
        if (customize == null) return "";
        return customize.reviewNotice();
    }


    public static class Customize {

        private String homeAmount;
        private String showSysReview;
        private String noPointRecord;
        private String companyAddress;
        private String mobile;
        private String company;
        private String afKey;
        private String email;
        private String repayType;
        private String rejectPeriod;
        private JsonArray walletChannels;
        private String transferDelayImportant;

        private String customerDesc;

        private SignMsg signMsg;

        private int minVersion;
        private int lastVersion;
        private String lastVersionName;
        private String lastVersionDesc;

        private String reviewNotice;
        private String loaningNotice;
        private String repayNotice;

        private Customize() {
        }

        public String getCustomerDesc() {
            return StringUtil_wooden.getSafeString_wooden(customerDesc);
        }

        public boolean showSysReview() {
            return StringUtil_wooden.getSafeString_wooden(showSysReview).equalsIgnoreCase("YES");
        }

        public String getHomeAmount() {
            return StringUtil_wooden.getSafeString_wooden(homeAmount);
        }

        public String getNoPointRecord() {
            return StringUtil_wooden.getSafeString_wooden(noPointRecord);
        }

        public String getCompanyAddress() {
            return StringUtil_wooden.getSafeString_wooden(companyAddress);
        }

        public String[] getMobile() {
            return StringUtil_wooden.getSafeString_wooden(mobile).split(",");
        }

        public String getCompany() {
            return StringUtil_wooden.getSafeString_wooden(company);
        }

        public String getAfKey() {
            return StringUtil_wooden.getSafeString_wooden(afKey);
        }

        public String[] getEmail() {
            return StringUtil_wooden.getSafeString_wooden(email).split(",");
        }

        public String getRepayType() {
            return StringUtil_wooden.getSafeString_wooden(repayType);
        }

        public String getRejectPeriod() {
            return StringUtil_wooden.getSafeString_wooden(rejectPeriod);
        }

        public JsonArray getWalletChannels() {
            return walletChannels;
        }

        public String getTransferDelayImportant() {
            return StringUtil_wooden.getSafeString_wooden(transferDelayImportant);
        }

        public int minVersion() {
            return minVersion;
        }

        public int lastVersion() {
            return lastVersion;
        }

        public String lastVersionName() {
            if (TextUtils.isEmpty(lastVersionName)) return "";
            return lastVersionName;
        }

        public String lastVersionDesc() {
            if (TextUtils.isEmpty(lastVersionDesc)) return "";
            return lastVersionDesc;
        }

        public String repayNotice() {
            if (TextUtils.isEmpty(repayNotice)) return "";
            return repayNotice;
        }

        public String loaningNotice() {
            if (TextUtils.isEmpty(loaningNotice)) return "";
            return loaningNotice;
        }

        public String reviewNotice() {
            if (TextUtils.isEmpty(reviewNotice)) return "";
            return reviewNotice;
        }

    }

    private void initWalletChannels_wooden() {
        walletChannels = new ArrayList<>();
        JsonArray jsonArray = customize.getWalletChannels();
        Gson gson = new Gson();
        if (jsonArray != null) {
            for (int i = 0; i < jsonArray.size(); i++) {
                WalletChannel walletChannel = gson.fromJson(jsonArray.get(i).toString(), WalletChannel.class);
                walletChannels.add(walletChannel);
            }
        }

        //获取图标
        ThreadUtil_wooden.getInstance_wooden().runOnChildThread_wooden(() -> {
            for (WalletChannel walletChannel : walletChannels) {
                try {
                    if (!TextUtils.isEmpty(walletChannel.getLogo())) {
                        URL url = new URL(walletChannel.getLogo());
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(5000);
                        connection.setRequestMethod("GET");
                        if (connection.getResponseCode() == 200) {
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            walletChannel.setBitmap(bitmap);
                        }
                    }
                } catch (IOException e) {
                    if (Constant_wooden.Debug) e.printStackTrace();
                }
            }
        });
    }

    public ArrayList<WalletChannel> getWalletChannels_wooden() {
        if (walletChannels == null) {
            initWalletChannels_wooden();
        }
        return walletChannels;
    }

    public static class WalletChannel {

        private String desc;
        private String value;
        private String logo;

        private Bitmap bitmap;

        public String getDesc() {
            return StringUtil_wooden.getSafeString_wooden(desc);
        }

        public String getValue() {
            return StringUtil_wooden.getSafeString_wooden(value);
        }

        public String getLogo() {
            return StringUtil_wooden.getSafeString_wooden(logo);
        }

        public Bitmap getBitmap() {
            return bitmap;
        }

        public void setBitmap(Bitmap bitmap) {
            this.bitmap = bitmap;
        }

    }

    public static class SignMsg {

        private String host;
        private String scheme;
        private String pck;
        private String appName;
        private String devName;
        private String logo;
        private String url;
        private String score;
        private String age;
        private String downloads;

        public String getHost() {
            return StringUtil_wooden.getSafeString_wooden(host);
        }

        public String getScheme() {
            return StringUtil_wooden.getSafeString_wooden(scheme);
        }

        public String getPck() {
            return StringUtil_wooden.getSafeString_wooden(pck);
        }

        public String getAppName() {
            return StringUtil_wooden.getSafeString_wooden(appName);
        }

        public String getDevName() {
            return StringUtil_wooden.getSafeString_wooden(devName);
        }

        public String getLogo() {
            return StringUtil_wooden.getSafeString_wooden(logo);
        }

        public String getUrl() {
            return StringUtil_wooden.getSafeString_wooden(url);
        }

        public String getScore() {
            return StringUtil_wooden.getSafeString_wooden(score);
        }

        public String getAge() {
            return StringUtil_wooden.getSafeString_wooden(age);
        }

        public String getDownloads() {
            return StringUtil_wooden.getSafeString_wooden(downloads);
        }
    }

}
